﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace Itop.YHPC {
    public class util {
        /// <summary>
        /// 将json中Unicode编码转换为汉字字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string U2G(string str) {
            
            string[] strs = str.Split(new string[]{"\\u"}, StringSplitOptions.None);
            StringBuilder sb = new StringBuilder();
            sb.Append(strs[0]);
            for (int i = 1; i < strs.Length; i++) {
                string s1 = strs[i];
                if (s1.Length >= 4) {
                    string s2 = s1.Substring(0, 4);
                    s1 = s1.Substring(4);
                    s2 = ToGB2312("\\u"+s2);
                    sb.Append(s2);
                }
                sb.Append(s1);
            }
            return sb.ToString();
        }
        /// <summary>
        /// 汉字转换为Unicode编码
        /// </summary>
        /// <param name="str">要编码的汉字字符串</param>
        /// <returns>Unicode编码的的字符串</returns>
        public static string ToUnicode(string str) {
            byte[] bts = Encoding.Unicode.GetBytes(str);
            string r = "";
            for (int i = 0; i < bts.Length; i += 2) r += "\\u" + bts[i + 1].ToString("x").PadLeft(2, '0') + bts[i].ToString("x").PadLeft(2, '0');
            return r;
        }
        /// <summary>
        /// 将Unicode编码转换为汉字字符串
        /// </summary>
        /// <param name="str">Unicode编码字符串</param>
        /// <returns>汉字字符串</returns>
        public static string ToGB2312(string str) {
            string r = "";
            MatchCollection mc = Regex.Matches(str, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            byte[] bts = new byte[2];
            foreach (Match m in mc) {
                bts[0] = (byte)int.Parse(m.Groups[2].Value, NumberStyles.HexNumber);
                bts[1] = (byte)int.Parse(m.Groups[1].Value, NumberStyles.HexNumber);
                r += Encoding.Unicode.GetString(bts);
            }
            return r;
        }
        public static string GetCompany() {

            return "哈尔滨智高点科技发展有限公司";
        }
        /// <summary>
        /// 检查登录有效性
        /// </summary>
        public static void CheckLogin() {
#if rabbit
            return ;
#endif
            string pathdic="";
            if(HttpContext.Current.Request.Path.Contains("yhpc")) pathdic = "/yhpc";
            if (HttpContext.Current.Session["user"] == null) {
                HttpContext.Current.Response.Write(@"<script>alert('你没有登陆或登陆超时!');top.location.href='"+pathdic+"/admin/Login.aspx';</script>");
                //HttpContext.Current.Response.Close();
            }
        }

        public static void Message(System.Web.UI.Page page, string nr) {

            page.RegisterStartupScript("message", "<script language='javascript' defer>alert('" + nr + "');</script>");
        }

        public static void Message1(System.Web.UI.Page page, string nr, string Url) {

            page.RegisterStartupScript("message", "<script language='javascript' defer>alert('" + nr + "'); location='" + Url + "'</script>");
        }
    }
}
